package com.canaan.business.service.bill.impl;

import java.util.Date;
import java.util.List;

import com.canaan.business.domain.bill.PickProductBill;
import com.canaan.business.service.basic.IWmsProductService;
import com.canaan.business.service.bill.IPickProductBillService;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.canaan.business.mapper.bill.PickProductBillDetailMapper;
import com.canaan.business.domain.bill.PickProductBillDetail;
import com.canaan.business.service.bill.IPickProductBillDetailService;

import javax.annotation.Resource;

/**
 * 领料单明细Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-30
 */
@Service
public class PickProductBillDetailServiceImpl implements IPickProductBillDetailService {
    @Resource
    private PickProductBillDetailMapper pickProductBillDetailMapper;
    @Resource
    private IWmsProductService productService;
    @Resource
    private IPickProductBillService pickProductBillService;


    /**
     * 查询领料单明细
     *
     * @param id 领料单明细主键
     * @return 领料单明细
     */
    @Override
    public PickProductBillDetail selectPickProductBillDetailById(Long id) {
        return pickProductBillDetailMapper.selectPickProductBillDetailById(id);
    }

    /**
     * 查询领料单明细列表
     *
     * @param receiveProductBillDetail 领料单明细
     * @return 领料单明细
     */
    @Override
    public List<PickProductBillDetail> selectPickProductBillDetailList(PickProductBillDetail receiveProductBillDetail) {
        return pickProductBillDetailMapper.selectPickProductBillDetailList(receiveProductBillDetail);
    }

    /**
     * 新增领料单明细
     *
     * @param pickProductBillDetail 领料单明细
     * @return 结果
     */
    @Override
    public int insertPickProductBillDetail(PickProductBillDetail pickProductBillDetail) {
        if (StringUtils.isBlank(pickProductBillDetail.getNo())) {
            pickProductBillDetail.setNo(TableNoUtil.getNo(PickProductBillDetail.class));
        }
        pickProductBillDetail.setCreateBy(SecurityUtils.getUsername());
        pickProductBillDetail.setCreateTime(DateUtils.getNowDate());
        int i = pickProductBillDetailMapper.insertPickProductBillDetail(pickProductBillDetail);
        this.refreshTotalQty(pickProductBillDetail.getBillNo());
        return i;
    }

    /**
     * 批量新增领料单明细
     *
     * @param pickProductBillDetailList 领料单明细列表
     * @return 结果
     */
    @Override
    public int batchInsertPickProductBillDetail(List<PickProductBillDetail> pickProductBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (PickProductBillDetail pickProductBillDetail : pickProductBillDetailList){
            if(StringUtils.isBlank(pickProductBillDetail.getNo())){
                pickProductBillDetail.setNo(TableNoUtil.getNo(PickProductBillDetail.class));
            }
            pickProductBillDetail.setCreateBy(username);
            pickProductBillDetail.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<pickProductBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > pickProductBillDetailList.size()) {
                endIndex = pickProductBillDetailList.size();
            }
            insertNum = insertNum + pickProductBillDetailMapper.batchInsertPickProductBillDetail(pickProductBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改领料单明细
     *
     * @param pickProductBillDetail 领料单明细
     * @return 结果
     */
    @Override
    public int updatePickProductBillDetail(PickProductBillDetail pickProductBillDetail) {
        pickProductBillDetail.setUpdateBy(SecurityUtils.getUsername());
        pickProductBillDetail.setUpdateTime(DateUtils.getNowDate());
        int i = pickProductBillDetailMapper.updatePickProductBillDetail(pickProductBillDetail);
        this.refreshTotalQty(pickProductBillDetail.getBillNo());
        return i;
    }

    private void refreshTotalQty(String billNo){
        PickProductBillDetail detail = new PickProductBillDetail();
        detail.setBillNo(billNo);
        List<PickProductBillDetail> details = this.selectPickProductBillDetailList(detail);
        PickProductBill pickProductBill = pickProductBillService.selectPickProductBillByNo(billNo);
        pickProductBill.setTotalQty(details.stream().filter(e->e.getQty()!=null).mapToLong(e -> e.getQty()).sum());
        pickProductBillService.updatePickProductBill(pickProductBill);
    }

    /**
     * 批量修改领料单明细
     *
     * @param receiveProductBillDetailList 领料单明细列表
     * @return 结果
     */
    @Override
    public int batchUpdatePickProductBillDetail(List<PickProductBillDetail> receiveProductBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (PickProductBillDetail receiveProductBillDetail : receiveProductBillDetailList){
            receiveProductBillDetail.setUpdateBy(username);
            receiveProductBillDetail.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<receiveProductBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > receiveProductBillDetailList.size()) {
                endIndex = receiveProductBillDetailList.size();
            }
            updateNum = updateNum + pickProductBillDetailMapper.batchUpdatePickProductBillDetail(receiveProductBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除领料单明细
     *
     * @param ids 需要删除的领料单明细主键集合
     * @return 结果
     */
    @Override
    public int deletePickProductBillDetailByIds(Long[] ids) {
        int i = pickProductBillDetailMapper.deletePickProductBillDetailByIds(ids);
        PickProductBillDetail pickProductBillDetail = this.selectPickProductBillDetailById(ids[0]);
        refreshTotalQty(pickProductBillDetail.getBillNo());
        return i;
    }

    /**
     * 删除领料单明细信息
     *
     * @param id 领料单明细主键
     * @return 结果
     */
    @Override
    public int deletePickProductBillDetailById(Long id) {
        return pickProductBillDetailMapper.deletePickProductBillDetailById(id);
    }

    /**
     * 查询领料单明细
     *
     * @param no 编号
     * @return 领料单明细
     */
    @Override
    public PickProductBillDetail selectPickProductBillDetailByNo(String no) {
        return pickProductBillDetailMapper.selectPickProductBillDetailByNo(no);
    }

    /**
     * 批量删除领料单明细
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deletePickProductBillDetailByNos(String[] nos) {
        return pickProductBillDetailMapper.deletePickProductBillDetailByNos(nos);
    }

    /**
     * 删除领料单明细信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deletePickProductBillDetailByNo(String no) {
        return pickProductBillDetailMapper.deletePickProductBillDetailByNo(no);
    }
}
